<?php
/* SVN FILE: $Id: item_changedisplayname.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Change Display Name item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/own/item_changedisplayname.php $
*/

class item_changedisplayname extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Change Your Display Name";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Change your your display name with this item. Changing display name with this item will bypass the limited changes you can make.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		if(!$this->settings['auth_allow_dnames'])
		{
			$this->registry->output->showError('must_enable_display_name');
		}
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @todo
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		if(!$this->settings['auth_allow_dnames'])
		{
			$this->registry->output->showError('must_enable_display_name');
		}
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_usercp' ), 'core' );
		
		if($this->request['request_method'] == 'post')
		{
			$name = trim($this->request['displayName']);
			$_seoName = IPSText::makeSeoTitle( $name );
			
			if(!$name)
			{
				$this->registry->getClass('output')->showError('complete_entire_form');
			}
			
			try
			{
				if ( IPSMember::getFunction()->checkNameExists( $name, $this->memberData, 'members_display_name', true ) === TRUE )
				{
					$this->registry->getClass('output')->showError('name_change_taken');
				}
				else
				{
					$this->DB->force_data_type = array( 'dname_previous'	=> 'string',
														'dname_current'		=> 'string' );
					
					$this->DB->insert( 'dnames_change', array( 'dname_member_id'		=> $this->memberData['member_id'],
																  'dname_date'		=> time(),
																  'dname_ip_address'	=> $this->memberData['ip_address'],
																  'dname_previous'	=> $this->memberData['members_display_name'],
																  'dname_current'		=> $name ) );
					
					//-----------------------------------------
					// Still here? Change it then
					//-----------------------------------------
					
					IPSMember::save( $this->memberData['member_id'], array( 'core' => array( 'members_display_name' => $name, 'members_l_display_name' => strtolower( $name ), 'members_seo_name' => $_seoName ) ) );
					
					$this->DB->force_data_type = array( 'last_poster_name' => 'string', 'seo_last_name' => 'string' );
					$this->DB->update( 'forums', array( 'last_poster_name' => $name, 'seo_last_name' => $_seoName ), "last_poster_id=" . $this->memberData['member_id'] );
					
					$this->DB->force_data_type = array( 'member_name' => 'string', 'seo_name' => 'string' );
					$this->DB->update( 'sessions', array( 'member_name' => $name, 'seo_name' => $_seoName ), "member_id=" . $this->memberData['member_id'] );
					
					$this->DB->force_data_type = array( 'starter_name' => 'string', 'seo_first_name' => 'string' );
					$this->DB->update( 'topics', array( 'starter_name' => $name, 'seo_first_name' => $_seoName ), "starter_id=" . $this->memberData['member_id'] );
					
					$this->DB->force_data_type = array( 'last_poster_name' => 'string', 'seo_last_name' => 'string' );
					$this->DB->update( 'topics', array( 'last_poster_name' => $name, 'seo_last_name' => $_seoName ), "last_poster_id=" . $this->memberData['member_id'] );
					
					//-----------------------------------------
					// Recache moderators
					//-----------------------------------------
					
					$this->registry->cache()->rebuildCache( 'moderators', 'forums' );
					
					//-----------------------------------------
					// Recache announcements
					//-----------------------------------------
					
					$this->registry->cache()->rebuildCache( 'announcements', 'forums' );
					
					//-----------------------------------------
					// Stats to Update?
					//-----------------------------------------
					
					$this->registry->cache()->rebuildCache( 'stats', 'core' );
					
					IPSLib::runMemberSync( 'onNameChange', $this->memberData['member_id'], $name );
				}
			}
			catch( Exception $error )
			{
				switch( $error->getMessage() )
				{
					case 'NO_NAME':
						$this->registry->getClass('output')->showError('name_change_tooshort');
					break;
					case 'TOO_LONG':
						$this->registry->getClass('output')->showError('name_change_tooshort');
					break;
					case 'ILLEGAL_CHARS':
						$this->registry->getClass('output')->showError('name_change_illegal');
					break;
					default:
						$this->registry->getClass('output')->showError($error->getMessage());
					break;
				}
			}
			
			$this->cache->rebuildCache( 'stats', 'global' );
			
			$this->deleteItems($invItem);
			
			$this->writeLog($invItem, "Changed display name from ".$this->memberData['members_display_name']." to ".$name);
			
			$this->lang->words['change_name_to_x'] = sprintf($this->lang->words['change_name_to_x'],$name);
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['change_name_to_x'], $this->settings['base_url_with_app'] . "module=inventory" );
		}
		
		return <<<EOF
<form id='ownDisplayNameForm' method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='ownDisplayName'class='block_wrap'>
		<h3 id='ownDisplayNameHeader' class='maintitle'>{$this->item['item_name']}</h3>
		<fieldset class="row2">
			<ul>
				<li>
					<label>{$this->lang->words['dname_choose']}</label>
					<input type='input' name='displayName' value='' size='30' class='input_text' maxlength='{$this->settings['max_user_name_length']}' />
				</li>
			</ul>
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['ucp__save_changes']}" name="submit" />
		</fieldset>
	</div>
</form>
EOF;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
